/*
 * @Description: 接收登记列表
 * @Author: Rfan
 * @Date: 2022-11-23 17:50:05
 * @LastEditTime: 2022-11-26 20:14:55
 */

import { useEffect, useMemo, useState } from 'react';
import { Button, Space, Table } from 'antd';
import classNames from 'classnames/bind';
import { isNullObj } from '@/utils';
import { useFormatMessage } from '@/utils/intlHelpers';
import { useBorrowContext } from '../contexts/BorrowContext';
import type { IDispositionPendingParams } from '@/api/archives/disposition';
import type { IPageQuery } from '@/interfaces/global';
import styles from '../styles/pageList.module.scss';
import { IRegBorrowListParams } from '@/api/registration/borrow';
import EditBorrowDialog from './dialogs/EditBorrowDialog';

const cx = classNames.bind(styles);
const { Column } = Table;

const PageList = (props: any) => {
  const { className } = props;
  const { list, selectItems, setSelectKey, getRegBorrowList, deleteItem } = useBorrowContext();
  const formatMessage = useFormatMessage();

  const [loading, setLoading] = useState(false);
  const [total, setTotal] = useState(0);
  const [listQuery, setListQuery] = useState<IPageQuery<IRegBorrowListParams>>({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });
  // 加入鉴定批次弹窗可视状态
  const [editVisible, setEditVisible] = useState(false);
  // 选中内容
  const [selectData, setSelectData] = useState(null);

  useEffect(() => {
    if (listQuery && listQuery.param) {
      setLoading(true);
      getRegBorrowList(listQuery)
        .then((res: any) => {
          setTotal(res?.totalCount);
        })
        .finally(() => {
          setLoading(false);
        });
    }
  }, [listQuery]);

  /**
   * @description 切换页码触发
   * @param {number} page 页码
   * @param {number} pageSize 每页条目数
   */
  const pageChange = (page: number, pageSize: number) => {
    setListQuery((state) => ({
      ...state,
      pageNo: page,
      pageSize
    }));
  };

  /**
   * @description : 表格选项相关设置
   * @param {number} selectedRowKeys 选择的项目的KEY
   */
  const onSelectChange = (selectedRowKeys: number[]) => {
    setSelectKey(selectedRowKeys);
  };

  /**
   * @description:  表格多选配置
   */
  const rowSelection = {
    selectItems,
    onChange: onSelectChange
  };

  const editItem = (item: any) => {
    setSelectData(item);
    setEditVisible(true);
  };

  return (
    <div className={className}>
      <Table
        dataSource={list}
        loading={loading}
        rowKey="id"
        scroll={{ x: 'max-content' }}
        bordered
        rowSelection={rowSelection}
        pagination={{
          current: listQuery.pageNo,
          pageSize: listQuery.pageSize,
          total,
          onChange: pageChange
        }}
      >
        <Column title="顺序号" dataIndex="id" key="id" align="center" />
        <Column title="日期" dataIndex="folderName" key="folderName" align="center" />
        <Column
          title="利用者姓名"
          dataIndex="borrowPersonName"
          key="borrowPersonName"
          align="center"
        />
        <Column title="利用者职位" dataIndex="archiveNo" key="archiveNo" align="center" />
        <Column title="利用者工作单位" dataIndex="unitName" key="unitName" />
        <Column title="利用目的" dataIndex="usePurpose" key="usePurpose" />
        <Column title="利用方式" dataIndex="usePattern" key="usePattern" />
        <Column title="借阅过程" dataIndex="borrowProcess" key="borrowProcess" />
        <Column title="归还时间" dataIndex="title" key="title" />
        <Column title="全宗" dataIndex="fondsName" key="fondsName" />
        <Column title="档案类型" dataIndex="archiveTypeName" key="archiveTypeName" />
        <Column title="档案号" dataIndex="title" key="title" />
        <Column title="案卷数量" dataIndex="folderCount" key="folderCount" />
        <Column title="文件数量" dataIndex="fileCount" key="fileCount" />
        <Column title="其它描述" dataIndex="remark" key="remark" />
        <Column
          title={formatMessage('tableAction')}
          key="action"
          className={cx('action-col')}
          align="center"
          render={(_text, record: any) => (
            <Space>
              <Button
                type="link"
                size="small"
                onClick={() => {
                  editItem(record);
                }}
              >
                编辑
              </Button>
              <Button
                type="link"
                size="small"
                onClick={() => {
                  deleteItem(record.id);
                }}
              >
                删除
              </Button>
            </Space>
          )}
        />
      </Table>
      {useMemo(
        () =>
          selectData && (
            <EditBorrowDialog
              visible={editVisible}
              data={selectData}
              onClose={() => {
                setEditVisible(false);
              }}
            />
          ),
        [editVisible]
      )}
    </div>
  );
};

export default PageList;
